package com.pinyougou.manage.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.pinyougou.http.Result;
import com.pinyougou.pojo.TbBrand;

import com.pinyougou.sellergoods.service.BrandService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/brand")
public class BrandController {

    @Reference
    private BrandService service;

   @ResponseBody
   @RequestMapping("/list")
    public PageInfo<TbBrand> list(@RequestBody TbBrand tbBrand,
                                  @RequestParam(value = "page",required = false,defaultValue ="1" )int page,
                                  @RequestParam(value = "size",required = false,defaultValue = "10") int size){
       PageInfo<TbBrand> all = service.getAll(tbBrand,page,size);
       System.out.println(all);
       return all;
   }

   @ResponseBody
   @RequestMapping("/findAll")
    public List<TbBrand> findAll(){
       return service.findAll();
   }

   @ResponseBody
   @RequestMapping("/addBrand.do")
   public Result addBrand(@RequestBody TbBrand tbBrand){
       try {
           int acount = service.addBrand(tbBrand);
           if (acount > 0) {
              return new Result(true,"增加品牌成功");
           }
       }catch (Exception e){
           e.printStackTrace();
       }
       return new Result(false,"失败");
   }

   @ResponseBody
    @RequestMapping("/findById.do")
    public TbBrand findById(@RequestParam(value = "id") long id){
       System.out.println(id);
       return service.findById(id);
   }

   @ResponseBody
    @RequestMapping("/updateBrand.do")
    public Result updateBrand(@RequestBody TbBrand tbBrand){
       System.out.println(tbBrand);
       try {
           int mcount = service.updateBrand(tbBrand);
           if (mcount>0){
               return new Result(true,"品牌修改成功");
           }
       }catch (Exception e){
           e.printStackTrace();
       }
       return new Result(false,"品牌修改失败");
   }

   @ResponseBody
    @RequestMapping("/delete")
    public Result delete(@RequestBody List<Long> ids){
       try {
           int mcount = service.delete(ids);
           if (mcount>0){
               return new Result(true,"品牌删除成功");
           }
       }catch (Exception e){
           e.printStackTrace();
       }
       return new Result(false,"品牌删除失败");

   }

}
